# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

library(tidyverse)
library(estimatr)
library(metafor)
library(broom)
library(coefplot)
source("code/helpers.R")

all_panels_long <- read_rds("data/all_panels_long.rds")

all_panels_long <-
  all_panels_long %>%
  mutate(timing = coalesce(correction_page_timer, misinfo_page_timer))

timings <-
  all_panels_long  %>%
  pivot_longer(
    cols = c(
      lucid_pid_3,
      grp_crt,
      grp_polint,
      grp_nfc,
      grp_openness,
      grp_conscientiousness,
      grp_extraversion,
      grp_agree,
      grp_emotstab
    )
  ) %>%
  mutate(name = factor(
    name,
    levels = c(
      "lucid_pid_3",
      "grp_pk",
      "grp_polint",
      "grp_crt",
      "grp_nfc",
      
      "grp_extraversion",
      "grp_agree",
      "grp_conscientiousness",
      "grp_emotstab",
      "grp_openness"
    ),
    c(
      "Party Affiliation",
      "Political knowledge",
      "Political interest",
      "Cognitive Reflection Test",
      "Need for Cognition",
      "Personality - Extraversion",
      "Personality - Agreeableness",
      "Personality - Conscientiousness",
      "Personality - Emotional Stability",
      "Personality - Openness"
    )
  )) %>%
  filter(!is.na(value),
         value != "med",
         value != "Independent",
         treatment != "control") %>%
  group_by(name, value, treatment, fc, misinfo_type, panel, panel_factor) %>%
  do(tidy(lm_robust(timing ~ 1, data = .)))

meta_timings <-
  timings %>%
  group_by(name, value, treatment) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE))


g1 <- 
ggplot(meta_timings, aes(estimate, name, group = value, shape = value)) +
  geom_point(position = position_dodgev(height = -0.3)) +
  geom_text(
    data = filter(meta_timings, treatment == "misinformation"),
    aes(label = value, x = 55),
    hjust = 0,
    position = position_dodgev(height = -0.4),
    size = 2
  ) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position = position_dodgev(height = -0.3)) +
  facet_wrap( ~ treatment) +
  theme_bw()  +
  theme(axis.title.y = element_blank(), 
        legend.position = "none",
        strip.background = element_blank()) +
  labs(x = "Average time spent with treatments, in seconds")

meta_timings_type <-
  timings %>%
  group_by(name, value, treatment, misinfo_type) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE))


g2 <- 
ggplot(meta_timings_type,
       aes(estimate, name, group = value, shape = value)) +
  geom_point(position = position_dodgev(height = -0.3)) +
  geom_text(
    data = filter(
      meta_timings_type,
      treatment == "misinformation",
      misinfo_type == "Misinformation is a social media post"
    ),
    aes(label = value, x = 65),
    size = 2, 
    hjust = 0,
    position = position_dodgev(height = -0.4)
  ) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position = position_dodgev(height = -0.3)) +
  facet_grid(misinfo_type ~ treatment) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        legend.position = "none",
        strip.background = element_blank())+
  labs(x = "Average time spent with treatments, in seconds")


g2
# ggsave("appendix_timing.pdf", g1, width = 6.5, height = 3.5)
# ggsave("appendix_timing_type.pdf", g2, width = 6.5, height = 6.5)



# write_rds(timings, file = "data/fitted_models/timings.rds")
# write_rds(meta_timings, file = "data/fitted_models/meta_timings.rds")




# 
# misinfo_timings <-
#   all_panels_long %>%
#   pivot_longer(
#     cols = c(
#       lucid_pid_3,
#       grp_crt,
#       grp_polint,
#       grp_nfc,
#       grp_openness,
#       grp_conscientiousness,
#       grp_extraversion,
#       grp_agree,
#       grp_emotstab
#     )
#   ) %>%
#   filter(!is.na(value),
#          value != "med",
#          value != "Independent",
#          !is.na(misinfo_page_timer)) %>%
#   group_by(name, value) %>%
#   do(tidy(lm_robust(misinfo_page_timer ~ 1, data = .)))



# correction_timings <-
#   correction_timings %>%
#   transmute(name, value, entry = make_se_entry(estimate, std.error)) %>%
#   pivot_wider(names_from = value, values_from = entry)
# 
# misinfo_timings <-
#   misinfo_timings %>%
#   transmute(name, value, entry = make_se_entry(estimate, std.error)) %>%
#   pivot_wider(names_from = value, values_from = entry)
# 
# 
# joined_table <- left_join(misinfo_timings, correction_timings, by = "name")
# 
# 
# 
# joined_table <-
#   joined_table %>%
#   mutate(name = factor(
#     name,
#     levels = c(
#       "grp_pk",
#       "grp_polint",
#       "grp_crt",
#       "grp_nfc",
#       
#       "grp_extraversion",
#       "grp_agree",
#       "grp_conscientiousness",
#       "grp_emotstab",
#       "grp_openness"
#     ),
#     c(
#       "Political knowledge",
#       "Political interest",
#       "Cognitive Reflection Test",
#       "Need for Cognition",
#       "Personality - Extraversion",
#       "Personality - Agreeableness",
#       "Personality - Conscientiousness",
#       "Personality - Emotional Stability",
#       "Personality - Openness"
#     )
#   )) %>%
#   arrange(name)
# 
# library(xtable)
# 
# 
# 
# joined_table %>%
#   xtable %>%
#   print.xtable(
#     include.colnames = FALSE,
#     include.rownames = FALSE,
#     hline.after = c(),
#     only.contents = TRUE,
#     file = "tables/timingstable.tex"
#   )
# 
# 


